function add_color(obj_id) {
    document.getElementById(obj_id).style.border="solid 1px #0c6fd9"
}

function del_color(obj_id) {
    document.getElementById(obj_id).style.border="solid 1px #efefef"
}

function change_mind(ele_id) {
    if (ele_id === "jurisdiction_list") {
        document.getElementById("jurisdiction_list").style.display = "inline-block";
        document.getElementById("add_jurisdiction").style.display = "none";
        document.getElementById("dispatch_detail_tab").className = "tab_top top_tag";
        document.getElementById("document_addressee_details_tab").className = "tab_top";
        document.getElementById("search_div").style.display = "block";
        document.getElementById("add_div").style.display = "none";
    } else if (ele_id === 'add_jurisdiction') {
        document.getElementById("add_jurisdiction").style.display = "inline-block";
        document.getElementById("jurisdiction_list").style.display = "none";
        document.getElementById("dispatch_detail_tab").className = "tab_top";
        document.getElementById("document_addressee_details_tab").className = "tab_top top_tag";
        document.getElementById("search_div").style.display = "none";
        document.getElementById("add_div").style.display = "block";
    }
}


function ModifyPermission(id=false) {
    var menu = document.getElementById("caidan").value;           // 菜单
    var bill = document.getElementById("danju").value;             // 单据
    var input_radio_list = $('input:radio');
    var groups = [];                                    // 权限组列表
    for (var i=0; i<input_radio_list.length; i++){
        if (input_radio_list[i].checked){
            groups.push(input_radio_list[i].name);
        }
    }
    var apply_user = document.getElementById("conferee").value;            // 执行人工号
    if (!menu){
        alert("请填写菜单名称！");
        return false;
    }else if (!bill){
        alert("请填写单据名称！");
        return false;
    }else if (!groups){
        alert("请选择权限组！");
        return false;
    }
    close_models();
    document.getElementById("loading").style.display = "inline-block";
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    xhr.open('get', "/personal-center/bill_group_view?page=" + String(1)+ '&id=' + String(id) + '&executor=' + String(apply_user) + '&groups=' + String(groups));
    xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
    xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
    xhr.send();
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                alert("修改成功！");
                window.location.reload();
            }
        }
    };
}

function close_models() {
    document.getElementById("new_models1").style.display='none';
}

function line_model(bill_id, menu, groups, bill, Executor) {
    padding_data1(menu, bill, groups, Executor);
    document.getElementById("new_models1").style.display='inline-block';
    document.getElementById("add_big_limits_model").setAttribute("onclick", "ModifyPermission('"+bill_id+"')");
}

function padding_data1(menu_name, bill_name, groups, job_numbers) {
    document.getElementById("caidan").value=menu_name;
    document.getElementById("caidan").setAttribute("disabled", "true");
    document.getElementById("danju").value=bill_name;
    document.getElementById("danju").setAttribute("disabled", "true")
    document.getElementById("conferee").value=job_numbers;

    groups = groups.split(", ")

    var input_radio_list = $('input:radio');
    for (var i=0; i<input_radio_list.length; i++){
            var a = groups.indexOf(input_radio_list[i].name)
            if(a > -1){
                input_radio_list[i].checked = true
            }else{
                input_radio_list[i].checked = false
            }
    }
};

function create_table1(json_data) {
    var t_table = document.getElementById("t_table1");
    var menu_name = "";
    var mwnu_num = 0;
    var menu_num = 1;
    for (var i = 0; i < json_data.length; i++) {
        var tr = document.createElement("tr");var td = document.createElement("td");var td1 = document.createElement("td");var td2 = document.createElement("td");var td3 = document.createElement("td");var td4 = document.createElement("td");
        if (json_data[i]["menu"] !== menu_name) {
            menu_name = json_data[i]["menu"];
            mwnu_num += 1;
            menu_num = 1;
            var tr_1 = document.createElement("tr");var td_1 = document.createElement("td");var td1_1 = document.createElement("td");var td2_1 = document.createElement("td");var td3_1 = document.createElement("td");
            td_1.innerHTML = '<a href="#" onclick="menu_contraction(\'' + "0" + String(mwnu_num) + '\', \'' + "0" + String(mwnu_num) + '\')"><i id="' + "0" + String(mwnu_num) + '" class="iconfont icon-sanjiaoyou"></i> ' + menu_name + '</a>';td_1.style.paddingLeft = "1%";
            td1_1.innerHTML = "0" + String(mwnu_num);td1_1.style.paddingLeft = "3.5%";
            td2_1.innerHTML = "有效";td2_1.style.textAlign = "center";
            td3_1.innerHTML = '<p onclick="model_chuxian(\''+json_data[i]["menu"]+'\')"><i  class="iconfont icon-zhucetianjiahaoyou"></i> 添加</p>';td3_1.style.textAlign = "center";td3_1.style.color = "#0c6fd9";td3_1.style.cursor = "pointer";
            tr_1.appendChild(td_1);tr_1.appendChild(td1_1);tr_1.appendChild(td2_1);tr_1.appendChild(td3_1);
            t_table.appendChild(tr_1);
            tr.className = "0" + String(mwnu_num);tr.style.display = "none";
            td.innerHTML = '<a href="#">' + json_data[i]["bill"] + '</a>';td.style.paddingLeft = "3%";
            td1.innerHTML = "0" + String(mwnu_num) + "-" + String(menu_num);td1.style.paddingLeft = "3.5%";
            td2.innerHTML = "有效";td2.style.textAlign = "center";
            td3.innerHTML = '/';td3.style.textAlign = "center";td3.style.color = "#0c6fd9";td3.style.cursor = "pointer";
            menu_num += 1
        } else {
            tr.className = "0" + String(mwnu_num);tr.style.display = "none";
            td.innerHTML = '<a href="#">' + json_data[i]["bill"] + '</a>';td.style.paddingLeft = "3%";
            td1.innerHTML = "0" + String(mwnu_num) + "-" + String(menu_num);td1.style.paddingLeft = "3.5%";
            td2.innerHTML = "有效";td2.style.textAlign = "center";
            td3.innerHTML = '/';td3.style.textAlign = "center";td3.style.color = "#0c6fd9";td3.style.cursor = "pointer";
            menu_num += 1
        }

        tr.appendChild(td);tr.appendChild(td1);tr.appendChild(td2);tr.appendChild(td3);t_table.appendChild(tr);
    }
}

function menu_contraction(contraction_num, Id_e) {
    var Issue_approval = document.getElementsByClassName(contraction_num);
    if (document.getElementById(Id_e).className === "iconfont icon-sanjiaoyou") {
        document.getElementById(Id_e).className = "iconfont icon-sanjiaoxia";
    } else {
        document.getElementById(Id_e).className = "iconfont icon-sanjiaoyou"
    }

    if (Issue_approval.length !== 0 && Issue_approval[0].style.display === "table-row") {
        for (var i = 0; i < Issue_approval.length; i++) {
            Issue_approval[i].style.display = "none";
        }
    } else {
        for (var j = 0; j < Issue_approval.length; j++) {
            Issue_approval[j].style.display = "table-row";
        }
    }
}

function model_chuxian(menu_name='') {
    document.getElementById("new_models1").style.display="inline-block";
    padding_data(menu_name);
}

function padding_data(menu_name='') {
    if (menu_name) {
        document.getElementById("caidan").value=menu_name;
    }else {
        document.getElementById("caidan").value='';
    }
    document.getElementById("caidan").removeAttribute("disabled")
    document.getElementById("danju").removeAttribute("disabled")
    document.getElementById("conferee").value="";
    document.getElementById("danju").value="";
    document.getElementById("modal-title").innerText = "增加权限";
    document.getElementById("add_big_limits_model").setAttribute("onclick", "add_permission()")
    var input_radio_list = $('input:radio');
    for (var i=0; i<input_radio_list.length; i++){
        input_radio_list[i].checked = false
    }
}

function add_permission() {
    var menu = document.getElementById("caidan").value;           // 菜单
    var bill = document.getElementById("danju").value;             // 单据
    var input_radio_list = $('input:radio');
    var groups = [];                                    // 权限组列表
    for (var i=0; i<input_radio_list.length; i++){
        if (input_radio_list[i].checked){
            groups.push(input_radio_list[i].name);
        }
    }
    var apply_user = document.getElementById("conferee").value;            // 执行人工号
    if (!menu){
        alert("请填写菜单名称！");
        return false;
    }else if (!bill){
        alert("请填写单据名称！");
        return false;
    }else if (!groups){
        alert("请选择权限组！");
        return false;
    }
    document.getElementById("loading").style.display = "inline-block";
    var xhr = new XMLHttpRequest();
    var local_host = window.location.host;          // 获取当前IP
    var form = new FormData();
    form.append("menu", String(menu));
    form.append("bill", String(bill));
    form.append("groups", groups);
    form.append("apply_user", String(apply_user));
    xhr.open('post', "/personal-center/bill_group_view", true);
    xhr.send(form);
    xhr.onreadystatechange = function () {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                document.getElementById("loading").style.display = "none";
                var JsonDate = JSON.parse(xhr.responseText);
                console.log('JsonDate', JsonDate);
                if (JsonDate['code'] !== 200){
                    alert(JsonDate['message'])
                }else {
                    alert("添加成功！");
                    window.location.reload();
                }
            }
        }
    };
}